//
//  FZRemoteNotificationParser.m
//  EnglishTalk
//
//  Created by 周咏 on 15/9/25.
//  Copyright (c) 2015年 Feizhu Tech. All rights reserved.
//  通知解析器
//

#import "FZRemoteNotificationParser.h"

#import <UIKit/UIKit.h>
#import "SRCommonConstants.h"
#import "FZUrlSchemesRouter.h"

@interface FZRemoteNotificationParser()
@end

@implementation FZRemoteNotificationParser

+(instancetype)sharedRemoteNotificationParser{
    static FZRemoteNotificationParser *instance;
    static dispatch_once_t once_t;
    dispatch_once(&once_t, ^(){
        instance = [[FZRemoteNotificationParser alloc] init];
    });
    return instance;
}

    //版本更新在前台提示
-(void)handleRemoteNotificationApplicationStateActive:(NSDictionary*)parameters{
    
//    if(!parameters && ![parameters objectForKey:@"type"]){
//        return;
//    }
//    NSString *type = [parameters objectForKey:@"type"];
    
}

-(BOOL)handleRemoteNotification:(NSDictionary *)parameters tabcontroller:(UITabBarController *)tabBarController{
    if(!parameters){
        return NO;
    }
    
    NSString *type = [NSString stringWithFormat:@"%@", [parameters objectForKey:@"type"]];
    
    NSDictionary *dataParams = nil;
    if([parameters objectForKey:@"data"] && [[parameters objectForKey:@"data"] isKindOfClass:[NSDictionary class]]){
        dataParams = [parameters objectForKey:@"data"];
    }
    
    return [self parseNotificationWithType:type parameters:dataParams tabBarController:tabBarController];
}


- (BOOL)parseNotificationWithType:(NSString *)type parameters:(NSDictionary *)parameters tabBarController:(UITabBarController *)tabBarController {
    if (!type || [type length] <= 0) {
        return NO;
    }
    
    Class classTypeController = nil;
    
//    if ([type isEqualToString:kPushNotificationContentTypeSystem] || [type isEqualToString:kPushNotificationContentTypeComments] || [type isEqualToString:kPushNotificationContentTypeFans]) {//系统消息，评论, 粉丝
//        tabBarController.selectedIndex = 3;
//        classTypeController= [FZMessageCenterViewController class];
//    }
//    else if([type isEqualToString:kPushNotificationContentTypeVersion]) {//版本更新
//        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:APPSTOREURL]];
//        return YES;
//    }
//    else if([type isEqualToString:kPushNotificationContentTypeBestshow]) {
//            //高手秀场列表
//        classTypeController = [FZBestShowViewController class];
//    }
    if(!classTypeController){
        return NO;
    }
    
    
    [self pushOrPresentViewController:NO paramteres:parameters classType:classTypeController tabcontroller:tabBarController];
    return YES;
}


/**
 *  跳转到对应的controller
 *
 *  @param isPresent
 *  @param paramteres
 *  @param classType
 *  @param tabBarController
 */
-(void)pushOrPresentViewController:(BOOL)isPresent paramteres:(NSDictionary*)paramteres classType:(Class)classType tabcontroller:(UITabBarController *)tabBarController
{
    UIViewController<FZUrlSchemesInitControllerDelegate> * controllerView=[[classType alloc] initControllerWithRemoteNotificationParameters:paramteres];
    
    if(!controllerView) {
        return;
    }
    
    UINavigationController *controllerNavigation = (UINavigationController *)[tabBarController selectedViewController];
    
    if (!isPresent) {
        
        controllerView.hidesBottomBarWhenPushed = YES;
        [controllerNavigation pushViewController:controllerView animated:YES];
    }
    else {
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controllerView];
        navigationController.viewControllers=@[controllerView];
        [controllerNavigation presentViewController:navigationController animated:NO completion:NULL];
    }
}

    //处理融云本地通知跳转
- (void)handleRongYunLocalNotification:(NSDictionary *)parameters tabBarController:(UITabBarController*)tabBarController
{
    if (!parameters) {
        return;
    }
    UINavigationController *controllerNavigation = (UINavigationController *)[tabBarController selectedViewController];
    Class ViewController=[[controllerNavigation.viewControllers lastObject] class];
    
    NSString *type = [parameters objectForKey:@"type"];

    if (!type) {
        return;
    }
    
//     if ([type isEqualToString:kPushNotificationContentTypeMessage]){
//             //私信
//        if (ViewController==[FZPersonalMessageViewController class]) {
//                //不需要跳转
//        }
//     }
//    else if ([type isEqualToString:kPushNotificationContentTypeBestshow]){
//            //切换到高手秀场
//        [controllerNavigation popToRootViewControllerAnimated:NO];
//        tabBarController.selectedIndex = 0;
//        [self pushOrPresentViewController:NO paramteres:parameters classType:[FZBestShowViewController class] tabcontroller:tabBarController];
//    }
}


    //处理融云的通知
- (BOOL)handleRongYunRemoteNotification:(NSDictionary *)parameters tabcontroller:(UITabBarController *)tabBarController
{
    NSString *dataInfoString =[parameters objectForKey:@"appData"];
    
    if (!dataInfoString) {
        return NO;
    }
    
    NSData *infoData = [dataInfoString dataUsingEncoding:NSUTF8StringEncoding];
    if (!infoData) {
        return NO;
    }

    NSDictionary *detailDict = [NSJSONSerialization JSONObjectWithData:infoData options:NSJSONReadingAllowFragments error:nil];
    if (!detailDict) {
        return NO;
    }
    
    NSString *type = [NSString stringWithFormat:@"%@", [detailDict objectForKey:@"type"]];
    
    return [self parseNotificationWithType:type parameters:detailDict tabBarController:tabBarController];
}



@end
